﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NFlickr.Factory;
using NFlickr.Resource;
using NFlickr.Core.Interface;

namespace NFlickr.Core
{
    public class FlickrUrl : ServiceBase
    {
        internal FlickrUrl()
        {
        }

        public override string MethodName
        {
            get { return "perms"; }
        }

        public override string TagName
        {
            get { return string.Empty; }
        }

        private string _url;

        public string Url
        {
            get
            {
                if (string.IsNullOrEmpty(this._url))
                {
                    if (Refresh())
                    {
                        return this._url;
                    }
                    else
                    {
                        return null;
                    }
                }
                return _url;
            }
        }

        public Frob Frob
        {
            get;
            set;
        }
        public Permission Permission
        {
            get;
            set;
        }

        public override bool RequireSigned
        {
            get { return false; }
        }

        public override bool Refresh()
        {
            Params = new Dictionary<string, string>();

            Params.Add(NFlickr.Resource.FlickrService.ApplicationKeyName, ApplicationKey);
            Params.Add(MethodName, Permission.ToString().ToLower());
            Params.Add(Frob.TagName, Frob.Value);

            IRESTServiceProvider RESTService = RESTServiceFactory.CreateInstance();
            IEncyptionProvider encypter = EncrptFactory.CreateInstance();
            string hash = RESTService.GetQueryString(Params,Secret);
            string signature = encypter.GetEncrypted(hash);
            Params.Add(NFlickr.Resource.FlickrService.SignatureKeyName, signature);

            string queryString = GetQueryString(Params);
            string url = NFlickr.Helper.Url.GetUrl(NFlickr.Resource.Url.LoginHost, NFlickr.Resource.Url.Port, NFlickr.Resource.Url.IsHttps, NFlickr.Resource.Url.LoginUrl);
            _url = url + "?" + queryString;
            return true;

        }
    }
}
